package com.example.seakdreamandroid.questions;



import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;

import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import android.view.View.OnClickListener;

import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.myInfo.MyInfo;

public class Questions extends Activity {
    private ImageView back;
    private LinearLayout test_layout;
    private Page the_page;
    //答案列表
    private ArrayList<Answer> the_answer_list;
    //问题列表
    private ArrayList<Question> the_question_list;
    //问题所在的View
    private View que_view;
    //答案所在的View
    private View ans_view;
    private LayoutInflater xInflater;
    private Page page;
    //下面这两个list是为了实现点击的时候改变图片，因为单选多选时情况不一样，为了方便控制
    //存每个问题下的imageview
    private ArrayList<ArrayList<ImageView>> imglist=new ArrayList<ArrayList<ImageView>>();
    //存每个答案的imageview
    private ArrayList<ImageView> imglist2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.questionnaire_activity);
        xInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //假数据
        initDate();
        //提交按钮
        Button button=(Button)findViewById(R.id.submit456);
        back = findViewById(R.id.test_back);
        button.setOnClickListener(new submitOnClickListener(page));
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void initDate() {
        //第一题
        Answer a_one=new Answer();
        a_one.setAnswerId("00");
        a_one.setAnswer_content("18岁以下");
        a_one.setAns_state(0);
        Answer a_two=new Answer();
        a_two.setAnswerId("01");
        a_two.setAnswer_content("18~25");
        a_two.setAns_state(0);
        Answer a_three=new Answer();
        a_three.setAnswerId("02");
        a_three.setAnswer_content("26~35");
        a_three.setAns_state(0);
        Answer a_four=new Answer();
        a_four.setAnswerId("03");
        a_four.setAnswer_content("35~50");
        a_four.setAns_state(0);
        Answer a_five=new Answer();
        a_five.setAnswerId("04");
        a_five.setAnswer_content("51~60");
        a_five.setAns_state(0);
        Answer a_six=new Answer();
        a_six.setAnswerId("05");
        a_six.setAnswer_content("60以上");
        a_six.setAns_state(0);

        //第二题
        Answer a_b=new Answer();
        a_b.setAnswerId("0");
        a_b.setAnswer_content("为自己选择，自己晚上常有失眠症状");
        a_b.setAns_state(0);

        Answer a_c=new Answer();
        a_c.setAnswerId("1");
        a_c.setAnswer_content("为自己的宝宝或婴儿选择，宝宝常常晚上哭闹，无法按时入眠");
        a_c.setAns_state(0);

        Answer a_d=new Answer();
        a_d.setAnswerId("2");
        a_d.setAnswer_content("为家中老人选择，老人受环境影响，晚上睡眠质量较差");
        a_d.setAns_state(0);

        //第三题
        Answer b_a=new Answer();
        b_a.setAnswerId("0");
        b_a.setAnswer_content("打游戏");
        b_a.setAns_state(0);

        Answer b_b=new Answer();
        b_b.setAnswerId("1");
        b_b.setAnswer_content("和朋友聊天");
        b_b.setAns_state(0);

        Answer b_c=new Answer();
        b_c.setAnswerId("2");
        b_c.setAnswer_content("学习");
        b_c.setAns_state(0);

        Answer b_d=new Answer();
        b_d.setAnswerId("3");
        b_d.setAnswer_content("在吃宵夜");
        b_d.setAns_state(0);

        Answer b_e=new Answer();
        b_e.setAnswerId("4");
        b_e.setAnswer_content("深夜emo");
        b_e.setAns_state(0);

        //第四题
        Answer c_a=new Answer();
        c_a.setAnswerId("0");
        c_a.setAnswer_content("生活环境嘈杂");
        c_a.setAns_state(0);

        Answer c_b=new Answer();
        c_b.setAnswerId("1");
        c_b.setAnswer_content("承受压力大，过于焦虑");
        c_b.setAns_state(0);

        Answer c_c=new Answer();
        c_c.setAnswerId("2");
        c_c.setAnswer_content("身体精神脆弱");
        c_c.setAns_state(0);

        Answer c_d=new Answer();
        c_d.setAnswerId("3");
        c_d.setAnswer_content("经常做梦");
        c_d.setAns_state(0);

        Answer c_e=new Answer();
        c_e.setAnswerId("4");
        c_e.setAnswer_content("个人兴趣爱好，如游戏，上网");
        c_e.setAns_state(0);

        //第5题
        Answer d_a=new Answer();
        d_a.setAnswerId("0");
        d_a.setAnswer_content("非常大");
        d_a.setAns_state(0);

        Answer d_b=new Answer();
        d_b.setAnswerId("1");
        d_b.setAnswer_content("一般");
        d_b.setAns_state(0);

        Answer d_c=new Answer();
        d_c.setAnswerId("2");
        d_c.setAnswer_content("不太大");
        d_c.setAns_state(0);

        Answer d_d=new Answer();
        d_d.setAnswerId("3");
        d_d.setAnswer_content("完全没有作用");
        d_d.setAns_state(0);

        //第6题
        Answer e_a=new Answer();
        e_a.setAnswerId("0");
        e_a.setAnswer_content("白噪音");
        e_a.setAns_state(0);

        Answer e_b=new Answer();
        e_b.setAnswerId("1");
        e_b.setAnswer_content("睡前故事");
        e_b.setAns_state(0);

        Answer e_c=new Answer();
        e_c.setAnswerId("2");
        e_c.setAnswer_content("相声");
        e_c.setAns_state(0);

        Answer e_d=new Answer();
        e_d.setAnswerId("3");
        e_d.setAnswer_content("戏曲");
        e_d.setAns_state(0);

        Answer e_e=new Answer();
        e_e.setAnswerId("4");
        e_e.setAnswer_content("冥想学习");
        e_e.setAns_state(0);

        Answer e_f=new Answer();
        e_f.setAnswerId("5");
        e_f.setAnswer_content("摇篮曲");
        e_f.setAns_state(0);

        Answer e_g=new Answer();
        e_g.setAnswerId("6");
        e_g.setAnswer_content("ASMR");
        e_g.setAns_state(0);

        Answer e_h=new Answer();
        e_h.setAnswerId("7");
        e_h.setAnswer_content("轻缓音乐");
        e_h.setAns_state(0);

        Answer e_i=new Answer();
        e_i.setAnswerId("8");
        e_i.setAnswer_content("其他");
        e_i.setAns_state(0);

        //第7题
        Answer f_a=new Answer();
        f_a.setAnswerId("0");
        f_a.setAnswer_content("放松心情");
        f_a.setAns_state(0);

        Answer f_b=new Answer();
        f_b.setAnswerId("1");
        f_b.setAnswer_content("陶冶情操");
        f_b.setAns_state(0);

        Answer f_c=new Answer();
        f_c.setAnswerId("2");
        f_c.setAnswer_content("亲近艺术");
        f_c.setAns_state(0);

        Answer f_d=new Answer();
        f_d.setAnswerId("3");
        f_d.setAnswer_content("逃避现实");
        f_d.setAns_state(0);

        Answer f_e=new Answer();
        f_e.setAnswerId("4");
        f_e.setAnswer_content("随便听听");
        f_e.setAns_state(0);

        Answer f_f=new Answer();
        f_f.setAnswerId("5");
        f_f.setAnswer_content("其他");
        f_f.setAns_state(0);


        ArrayList<Answer> answers_one=new ArrayList<Answer>();
        answers_one.add(a_one);
        answers_one.add(a_two);
        answers_one.add(a_three);
        answers_one.add(a_four);
        answers_one.add(a_five);
        answers_one.add(a_six);

        ArrayList<Answer> answers_two=new ArrayList<Answer>();
        answers_two.add(a_b);
        answers_two.add(a_c);
        answers_two.add(a_d);

        ArrayList<Answer> answers_three=new ArrayList<Answer>();
        answers_three.add(b_a);
        answers_three.add(b_b);
        answers_three.add(b_c);
        answers_three.add(b_d);
        answers_three.add(b_e);

        ArrayList<Answer> answers_four=new ArrayList<Answer>();
        answers_four.add(c_a);
        answers_four.add(c_b);
        answers_four.add(c_c);
        answers_four.add(c_d);
        answers_four.add(c_e);

        ArrayList<Answer> answers_five=new ArrayList<Answer>();
        answers_five.add(d_a);
        answers_five.add(d_b);
        answers_five.add(d_c);
        answers_five.add(d_d);

        ArrayList<Answer> answers_six=new ArrayList<Answer>();
        answers_six.add(e_a);
        answers_six.add(e_b);
        answers_six.add(e_c);
        answers_six.add(e_d);
        answers_six.add(e_e);
        answers_six.add(e_f);
        answers_six.add(e_g);
        answers_six.add(e_h);
        answers_six.add(e_i);

        ArrayList<Answer> answers_seven=new ArrayList<Answer>();
        answers_seven.add(f_a);
        answers_seven.add(f_b);
        answers_seven.add(f_c);
        answers_seven.add(f_d);
        answers_seven.add(f_e);
        answers_seven.add(f_f);


        Question q_one=new Question();
        q_one.setQuestionId("00");
        q_one.setType("0");
        q_one.setContent("1、年龄？");
        q_one.setAnswers(answers_one);
        q_one.setQue_state(0);

        Question q_two=new Question();
        q_two.setQuestionId("01");
        q_two.setType("0");//type=1,为多选题，不能把type设置为1
        q_two.setContent("2、请问您符合下面那种情况？");
        q_two.setAnswers(answers_two);
        q_two.setQue_state(0);

        Question q_three=new Question();
        q_three.setQuestionId("02");
        q_three.setType("1");
        q_three.setContent("3、晚睡原因？【请选择1-3项】");
        q_three.setAnswers(answers_three);
        q_three.setQue_state(0);

        Question q_four=new Question();
        q_four.setQuestionId("03");
        q_four.setType("1");
        q_four.setContent("4、您认为是因为哪些因素降低了您的睡眠质量？【最少选择4项】");
        q_four.setAnswers(answers_four);
        q_four.setQue_state(0);

        Question q_five=new Question();
        q_five.setQuestionId("04");
        q_five.setType("0");
        q_five.setContent("5、您认为音乐对于心里健康引导作用大吗");
        q_five.setAnswers(answers_five);
        q_five.setQue_state(0);

        Question q_six=new Question();
        q_six.setQuestionId("05");
        q_six.setType("1");
        q_six.setContent("6、在睡前喜欢哪种类型的助眠音乐？");
        q_six.setAnswers(answers_six);
        q_six.setQue_state(0);

        Question q_seven=new Question();
        q_seven.setQuestionId("06");
        q_seven.setType("0");
        q_seven.setContent("7、您为什么喜欢音乐？");
        q_seven.setAnswers(answers_seven);
        q_seven.setQue_state(0);

        ArrayList<Question> questions=new ArrayList<Question>();
        questions.add(q_one);
        questions.add(q_two);
        questions.add(q_three);
        questions.add(q_four);
        questions.add(q_five);
        questions.add(q_six);
        questions.add(q_seven);

        page=new Page();
        page.setPageId("000");
        page.setStatus("0");
        page.setTitle("大学生睡眠情况调查表");
        page.setQuestions(questions);
        //加载布局
        initView(page);
    }
    private void initView(Page page) {

        test_layout=(LinearLayout)findViewById(R.id.lly_test);
        TextView page_txt=(TextView)findViewById(R.id.txt_title);
        page_txt.setText(page.getTitle());

        the_question_list=page.getQuestions();

        for(int i=0;i<the_question_list.size();i++){
            que_view=xInflater.inflate(R.layout.question_layout, null);
            TextView txt_que=(TextView)que_view.findViewById(R.id.txt_question_item);

            LinearLayout add_layout=(LinearLayout)que_view.findViewById(R.id.lly_answer);

            if(the_question_list.get(i).getType().equals("1")){
                set(txt_que,the_question_list.get(i).getContent(),1);
            }else{
                set(txt_que,the_question_list.get(i).getContent(),0);
            }

            the_answer_list=the_question_list.get(i).getAnswers();
            imglist2=new ArrayList<ImageView>();
            for(int j=0;j<the_answer_list.size();j++){
                ans_view=xInflater.inflate(R.layout.answer_layout, null);
                TextView txt_ans=(TextView)ans_view.findViewById(R.id.txt_answer_item);
                ImageView image=(ImageView)ans_view.findViewById(R.id.image123);
                View line_view=ans_view.findViewById(R.id.vw_line);
                if(j==the_answer_list.size()-1){

                    line_view.setVisibility(View.GONE);
                }

                if(the_question_list.get(i).getType().equals("1")){
                    image.setBackgroundDrawable(getResources().getDrawable(R.drawable.double_white));
                }else{
                    image.setBackgroundDrawable(getResources().getDrawable(R.drawable.single_white));
                }
                Log.e("---", "------"+image);
                imglist2.add(image);
                txt_ans.setText(the_answer_list.get(j).getAnswer_content());
                LinearLayout lly_answer_size=(LinearLayout)ans_view.findViewById(R.id.lly_answer_size);
                lly_answer_size.setOnClickListener(new answerItemOnClickListener(i,j,the_answer_list,txt_ans));
                add_layout.addView(ans_view);
            }
			/*for(int r=0; r<imglist2.size();r++){
				Log.e("---", "imglist2--------"+imglist2.get(r));
			}*/

            imglist.add(imglist2);

            test_layout.addView(que_view);
        }
		/*for(int q=0;q<imglist.size();q++){
			for(int w=0;w<imglist.get(q).size();w++){
				Log.e("---", "共有------"+imglist.get(q).get(w));
			}
		}*/

    }
    private void set(TextView tv_test, String content,int type) {

        String w;
        if(type==1){
            w = content+"*[多选题]";
        }else{
            w = content+"*[单选题]";
        }

        int start = content.length();
        int end = w.length();
        Spannable word = new SpannableString(w);
        word.setSpan(new AbsoluteSizeSpan(40), start, end,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        word.setSpan(new StyleSpan(Typeface.BOLD), start, end,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        word.setSpan(new ForegroundColorSpan(Color.RED), start, end,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        tv_test.setText(word);
    }
    class answerItemOnClickListener implements OnClickListener{
        private int i;
        private int j;
        private TextView txt;
        private ArrayList<Answer> the_answer_lists;
        public answerItemOnClickListener(int i,int j, ArrayList<Answer> the_answer_list,TextView text){
            this.i=i;
            this.j=j;
            this.the_answer_lists=the_answer_list;
            this.txt=text;

        }

        //@Override
        public void onClick(View arg0) {



            if(the_question_list.get(i).getType().equals("1")){
                //多选
                if(the_answer_lists.get(j).getAns_state()==0){
                    //如果未被选中
                    //txt.setTextSize(22);
                    //txt.setTextColor(Color.parseColor("#EA5514"));
                    imglist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.double_black));
                    the_answer_lists.get(j).setAns_state(1);
                    the_question_list.get(i).setQue_state(1);
                }else{
                    //txt.setTextSize(22);
                   // txt.setTextColor(Color.parseColor("#595757"));
                    imglist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.double_white));
                    the_answer_lists.get(j).setAns_state(0);
                    the_question_list.get(i).setQue_state(1);
                }
                int h=0;
                for(int z=0;z<the_answer_lists.size();z++)
                {
                    if(the_answer_lists.get(z).getAns_state() == 1){
                         h = 1;
                    }
                }
                if (h==0){
                    the_question_list.get(i).setQue_state(0);
                };

            }else{
                //单选

                for(int z=0;z<the_answer_lists.size();z++)
                {
                the_answer_lists.get(z).setAns_state(0);
                //txt.setTextColor(Color.parseColor("#595757"));
                imglist.get(i).get(z).setBackgroundDrawable(getResources().getDrawable(R.drawable.single_white));
                }
                if(the_answer_lists.get(j).getAns_state()==0){
                    //如果当前未被选中
                    //txt.setTextSize(22);
                   // txt.setTextColor(Color.parseColor("#EA5514"));
                    imglist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.single_black));
                    the_answer_lists.get(j).setAns_state(1);
                    the_question_list.get(i).setQue_state(1);
                }else{
                    //如果当前已被选中
                    //txt.setTextSize(22);
                    //txt.setTextColor(Color.parseColor("#595757"));
                    imglist.get(i).get(j).setBackgroundDrawable(getResources().getDrawable(R.drawable.single_white));
                    the_answer_lists.get(j).setAns_state(0);
                    the_question_list.get(i).setQue_state(1);
                }

            }
        }

    }
    class submitOnClickListener implements OnClickListener{
        private Page page;
        public submitOnClickListener(Page page){
            this.page=page;
        }
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            boolean isState=true;
            //最终要的json数组
            JSONArray jsonArray = new JSONArray();
            //点击提交的时候，先判断状态，如果有未答完的就提示，如果没有再把每条答案提交（包含问卷ID 问题ID 及答案ID）

            for(int i=0;i<the_question_list.size();i++){
                the_answer_list=the_question_list.get(i).getAnswers();
                //判断是否有题没答完
                if(the_question_list.get(i).getQue_state()==0){
                    Toast.makeText(getApplicationContext(), "您第"+(i+1)+"题没有答完", Toast.LENGTH_LONG).show();
                    jsonArray=null;
                    isState=false;
                    break;
                }else{
                    for(int j=0;j<the_answer_list.size();j++){
                        if(the_answer_list.get(j).getAns_state()==1){
                            JSONObject json = new JSONObject();
                            try {

                                json.put("questionId", the_question_list.get(i).getQuestionId());
                                json.put("optionId", the_answer_list.get(j).getAnswerId());
                                jsonArray.put(json);
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }

            }
            if(isState){
                if(jsonArray.length()>0){
                    Log.e("af", jsonArray.toString());
                    AlertDialog alertDialog2 = new AlertDialog.Builder(Questions.this)
                            .setTitle("提示")
                            .setMessage("提交成功")
                            .setIcon(R.mipmap.ic_launcher)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加"Yes"按钮
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {

                                }
                            })
                            .create();


                    alertDialog2.show();
                   /* for(int item=0;item<jsonArray.length();item++){
                        JSONObject job;
                        try {
                            job = jsonArray.getJSONObject(item);
                            Log.e("----", "pageId--------"+job.get("pageId"));
                            Log.e("----", "questionId--------"+job.get("questionId"));
                            Log.e("----", "answerId--------"+job.get("answerId"));
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }  // 遍历 jsonarray 数组，把每一个对象转成 json 对象

                    }*/

                }

            }





        }
    }

}
